const ip = 'https://txxb.soft.haoyangsoft.com'

const baseURL = 'https://txxb.soft.haoyangsoft.com/static/wx'; // 图片代理地址

class Http {
	request(url, data = {}, method = "GET") {
		return new Promise((reslove, reject) => {
			wx.showLoading({
				title: '加载中'
			})
			return wx.request({
				url: ip + url,
				method: method,
				data: {
					...data
				},
				dataType: 'json',
				header: {
					'Authorization': wx.getStorageSync('token')
				},
				success(res) {
					const { code, msg } = res.data
					if (code == 200 || code == 505) {
						wx.hideLoading()
						reslove(res.data)
					} else if (code == 403) {
						wx.hideLoading()
						wx.removeStorageSync('token')
						wx.removeStorageSync('id')
						wx.showToast({
							title: msg,
							icon: "none",
						})
						setTimeout(() => {
							wx.navigateTo({
								url: '/pages/my/login/login'
							})
						}, 1500);
					} else {
						wx.showToast({
							title: msg,
							icon: "none",
							duration:2000
						})
				
						reslove(res.data)
					}
				},
				fail(error) {
					wx.showToast({
						title: '抱歉,网络错误',
						icon: "none"
					})
					reject(error);
				},
				complete() {
					//
				}
			})
		})
	}
}

module.exports = {
	Http,
	baseURL,
	ip
}

